package com.ruoyi.test.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.filter.GenericFilterBean;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@Slf4j
public class Token认证Filter extends GenericFilterBean {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse = (HttpServletResponse) response;
        log.info(httpRequest.getRequestURI());
        if(true){
            chain.doFilter(request, response);
        }else{
            httpResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        }

     /*   // 从请求头中获取授权信息
        String authorizationHeader = httpRequest.getHeader("Authorization");

        // 检查授权信息是否存在且以Bearer开头
        if (StringUtils.hasText(authorizationHeader) && authorizationHeader.startsWith("Bearer ")) {
            // 提取令牌
            String token = authorizationHeader.substring(7);

            // 在此处执行身份验证逻辑
            if (validateToken(token)) {
                // 身份验证成功，继续处理请求
                chain.doFilter(request, response);
            } else {
                // 身份验证失败，返回未授权错误
                httpResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
            }
        } else {
            // 授权信息不合法，返回未授权错误
            httpResponse.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
        }*/
    }

    private boolean validateToken(String token) {
        // 在此处进行身份验证逻辑，例如验证JWT的有效性
        // 如果验证通过，返回true；否则返回false
        // 根据实际情况进行实现
        // ...
        return true;
    }

}